﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using JKF.DataModule.IDAL;

namespace JKF.DataModule.SqlServerDAL
{
    /// <summary>
    /// 数据访问类dalSMS_TASK。
    /// </summary>
    public partial class dalSMS_TASK : IdalSMS_TASK
    {
        /// <summary>
        /// 获得短信任务列表
        /// </summary>
        /// <param name="currentUser"></param>
        /// <param name="pageCurrent"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageCount"></param>
        /// <param name="rowCount"></param>
        /// <returns></returns>
        public static DataTable GetSmsTaskPaging(string currentUser, int pageCurrent, int pageSize, ref int pageCount, ref int rowCount)
        {
            var sql = new StringBuilder("");
            sql.AppendFormat(" shop_code in(select shop_code from sys_user_shop where user_key='{0}') and STATUS<>9", currentUser);
            return DataPage.GetPageInfo("SMS_TASK", "TASK_KEY", pageCurrent, pageSize, "TASK_KEY,RECEIVER,CONTENT,PERIOD_TYPE,STATUS,PERIOD_VALUE", "", sql.ToString(), ref pageCount, ref rowCount).Tables[0];

        }

        /// <summary>
        /// 获取短信任务执行日志
        /// </summary>
        /// <param name="taskKey"></param>
        /// <param name="dateStart"></param>
        /// <param name="dateEnd"></param>
        /// <param name="pageCurrent"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageCount"></param>
        /// <param name="rowCount"></param>
        /// <returns></returns>
        public static DataTable GetSmsTaskLog(string taskKey, DateTime? dateStart, DateTime? dateEnd, int pageCurrent, int pageSize, ref int pageCount, ref int rowCount)
        {
            var sql = new StringBuilder("");
            sql.AppendFormat("a.task_key='{0}'", taskKey);
            if (dateStart.HasValue) sql.AppendFormat("  and a.DATE_CREATE>='{0}'", dateStart.Value.ToString("yyyy-MM-dd"));
            if (dateEnd.HasValue) sql.AppendFormat("  and a.DATE_CREATE<='{0}'", dateEnd.Value.ToString("yyyy-MM-dd") + " 23:59:59");
            return DataPage.GetPageInfo(" SMS_TASK_LOG a left join SMS_SEND_LOG b on a.TASK_KEY=b.TASK_KEY inner join SMS_TASK c on a.TASK_KEY=c.TASK_KEY group by b.TASK_KEY, c.CONTENT,a.DATE_CREATE", "a.TASK_KEY", pageCurrent, pageSize, " c.CONTENT ,a.DATE_CREATE,count(*) as SMS_COUNT ", "", sql.ToString(), ref pageCount, ref rowCount).Tables[0];
        }

    }
}
